library(htmltools)

APP_URL <- "https://pchen-aitutor.hf.space"  # ← your Gradio app URL
USER_ID <- "anonymous_user"

browsable(tagList(
  # --- styles (fab, bubble, modal) ---
  tags$style(HTML("
    #tutor-fab{position:fixed;right:20px;bottom:20px;z-index:99999;padding:12px 14px;border-radius:999px;border:1px solid #ddd;background:#fff;box-shadow:0 8px 24px rgba(0,0,0,.15);cursor:pointer;font-weight:600}
    #tutor-fab:hover{background:#f6f8ff}
    #tutor-sel-btn{position:absolute;display:none;z-index:100000;padding:6px 10px;font-size:12px;border:1px solid #e5e7eb;border-radius:10px;background:#111827;color:#f9fafb;box-shadow:0 8px 24px rgba(0,0,0,.2);cursor:pointer;user-select:none}
    #tutor-modal-overlay{display:none;position:fixed;z-index:99998;inset:0;background:rgba(0,0,0,.35)}
    #tutor-modal{position:absolute;top:5%;left:50%;transform:translateX(-50%);width:min(980px,95vw);height:min(720px,90vh);background:#fff;border-radius:10px;box-shadow:0 10px 30px rgba(0,0,0,.25);overflow:hidden}
    #tutor-modal-header{display:flex;justify-content:space-between;align-items:center;padding:10px 14px;background:#f5f5f7;border-bottom:1px solid #e5e5ea;font-weight:600}
    #tutor-close-btn{cursor:pointer;border:none;background:transparent;font-size:18px}
    #tutor-iframe{width:100%;height:calc(100% - 18px);border:0}
  ")),

  # modal + iframe
  tags$div(id="tutor-modal-overlay",
    tags$div(id="tutor-modal",
      tags$div(id="tutor-modal-header","AI Calculus Tutor", tags$button(id="tutor-close-btn","✕")),
      tags$iframe(id="tutor-iframe", src="")
    )
  ),

  # floating button + selection bubble
  tags$button(id="tutor-fab","Ask Tutor"),
  tags$button(id="tutor-sel-btn","Ask Tutor"),

  # script
  tags$script(HTML(sprintf("
    (function(){
      const APP_URL = '%s';
      const USER_ID = '%s';

      function sel(){ try { return (window.getSelection?window.getSelection().toString():'').trim(); } catch(e){ return ''; } }
      function openInline(q, action){
        const p = new URLSearchParams({ user_id: USER_ID, q: q||'', action: action||'prefill' });
        const frame = document.getElementById('tutor-iframe');
        frame.src = APP_URL + '/?' + p.toString();          // Prefill handled by app from URL
        document.getElementById('tutor-modal-overlay').style.display='block';
      }
      function openTab(q, action){
        const p = new URLSearchParams({ user_id: USER_ID, q: q||'', action: action||'prefill' });
        window.open(APP_URL + '/?' + p.toString(), 'ai_tutor_tab', 'noopener');
      }

      // floating button: prefill by default; Alt=send; Ctrl/Cmd=new tab
      document.getElementById('tutor-fab').addEventListener('click', function(ev){
        const q = sel();
        const act = ev.altKey ? 'send' : 'prefill';
        if (ev.ctrlKey || ev.metaKey) openTab(q, act); else openInline(q, act);
      });

      // selection bubble near highlighted text
      const b = document.getElementById('tutor-sel-btn');
      let last='';
      function place(x,y){ b.style.left=(x+10)+'px'; b.style.top=(y+10)+'px'; }
      function show(){ b.style.display='block'; } function hide(){ b.style.display='none'; }
      document.addEventListener('mouseup', e => setTimeout(() => {
        const t = sel(); if (t && t.length>=2 && t.length<=800){ last=t; place(e.pageX,e.pageY); show(); } else hide();
      }, 20));
      window.addEventListener('scroll', hide, {passive:true}); window.addEventListener('resize', hide);

      // bubble click: prefill by default; Alt=send; Ctrl/Cmd=new tab
      b.addEventListener('click', function(ev){
        if (!last) return;
        const act = ev.altKey ? 'send' : 'prefill';
        if (ev.ctrlKey || ev.metaKey) openTab(last, act); else openInline(last, act);
        hide();
      });

      // close modal
      const overlay = document.getElementById('tutor-modal-overlay');
      document.getElementById('tutor-close-btn').addEventListener('click', () => overlay.style.display='none');
      overlay.addEventListener('click', e => { if (e.target===overlay) overlay.style.display='none'; });
    })();
  ", APP_URL, USER_ID)))
))
AI Calculus Tutor

Learning Objectives

By the end of this 90-minute session, students will be able to:

  1. Define and plot univariable functions in R for economic applications
  2. Define and plot multivariable functions in R for business optimization
  3. Solve systems of linear equations using matrix notation and operations
  4. Analyze quadratic functions and positive definite matrices with their graphical representations
  5. Using Graphs to understand multivariable linear and quadratic functions
  6. Compute and interpret eigenvalues and eigenvectors for matrices (stable patterns under matrix transformations)

R skills:

  1. Define a function using the R keyword function()
  2. Define a function that uses another function as input
  3. Create 3D visualisations of multivariable functions
  4. Solving linear equation systems using R
  5. Compute eigenvalues and eigenvectors using eigen() (and verify \(A\mathbf{v}=\lambda\mathbf{v}\))

Knowledge Point 1: Defining and Plotting Univariable Functions in R

Example 2.1: The Coffee Shop Economics

Bean & Brew Café wants to optimize its operations using mathematical modeling. The owner needs to understand:

  1. Production function: \(Q(L) = 10\sqrt{L}\) (cups per hour vs labor hours)
  2. Cost function: \(C(Q) = 50 + 2Q + 0.1Q^2\) (total cost vs output)
  3. Revenue function: \(R(Q) = 5Q - 0.05 Q^2\) (revenue vs quantity sold)
  4. Profit function: \(\Pi(Q) = R(Q) - C(Q)\) (profit optimization)

How to formulate and depict a functional relationship

  • Is the relationship positive or negative?
  • Is it linear or nonlinear?
  • If it is nonlinear, does the rate of change increase or decrease as the input increases?
  • Does it describe a growth/decay process over time?
  • Is the relationship cyclic (periodic)?

How to plot functions in R

In R, functions can be defined using several approaches:

Method 1: Vectorised operations in R

By vectorised operations we mean that the input \(x\) is a sequence of real numbers (a vector). Applying an expression to a vector produces a new vector \(y\): each value in \(x\) is mapped to exactly one value in \(y\).

R code for a function defined using vectorised operations is as follows:

# x <- seq(from, to, by = step)
# y <- expression_in_x

## Example

x <- seq(0,10,0.1)

y = 0.5*x + 2

plot(x,y,type = "l", main = "Linear Function Y = 0.5 X + 2", xlab = "X", ylab = "Y")

Method 2: Using function() keyword

R code using function() is like follows:

For \[y = 0.5x + 2\]

The following R code defines the function and plot its graph.

#  f <- function(x) {
#     return(expression_in_x)
#  }

## Example

f <- function(x) {
  return(0.5*x+2)
}
x <- seq(0,10,0.1)
y <- f(x)

plot(x, y, type = "l", main = "Linear Function Y = 0.5 X + 2", xlab = "X", ylab = "Y")

Vectorised operations are often simple, but they are less flexible and are not applicable in some situations.

Example 1.2 The delivery service (revisited)

The price is a piecewise linear function.

\[C(d) = \begin{cases} 10 & \text{if } 0 \leq d \leq 5 \\ 10 + 2(d-5) & \text{if } 5 < d \leq 15 \\ 10 + 20 + 1.5(d-15) & \text{if } d > 15 \end{cases}\]

In this case we can only use function() to define the price function in R.

C<-function(d){
  if ((d>0)&( d <= 5))  {price<-10}
  if ((d>5)&( d <= 15)) {price<- 10 + 2*(d-5)}
  if (d>15)             {price <- 10 + 20 + 1.5*(d-15)}
  return(price)
}

d<- seq(1,30)
P<-d*0
# We cannot do P <- C(d) because the if-conditions in C(d) assume that d is a single number (not a vector).
# Instead, we compute the price for each distance value one-by-one.
for (i in 1:length(d)) {
  P[i] = C(d[i])
}

plot(d,P,type = "l", main = "Price of Delivery", 
     xlab = "Distance (km)", ylab = "Price")

Exercise 1

Use the R keyword function() to define a function that takes another function \(f\) as an input and plots its graph over a chosen domain. Then test your plotting function by graphing each of the following functions (choose a suitable domain for each):

  • A linear function that passes through the point \((x_0,y_0)= (2,3)\) and has slope \(b=1.5\).

  • \(f(x) = \sin(2x)\)

  • \(f(x)=\frac{1}{1+e^{-x}}\)

  • \(f(x) = \frac{x^3+1}{5+x^2}\)

The code snippets above can be used as templates to complete this exercise.

Exercise 2

Use the R keyword function() to define the following function and draw its graph over the domain \(x\in[0,10]\) in the R environment below (or in your own local RStudio). You can copy the sample code above and modify it to define the function.

\[f(x) = \begin{cases} 2x & \text{if } 0 \leq x \leq 5 \\ 10 + 4(x-5) & \text{if } 5 < x \\ \end{cases}\]

Plot a function using the function as input

In R, a function can take many types of inputs, including another function. Here is an example of a plotting function that takes a function f and a vector x as inputs, and then plots \(f(x)\) over the domain specified by x.

Plot_function <- function(f, x) {
  y <- f(x)
  plot(x, y, type = "l")
}

### Note: Plot_function can be used to plot any function of $x$. See the following two examples.

## Define a function Line_point_slope: it passes through (x0, y0) with slope b
x0 = 3
y0 = 4
b  = 2
Line_point_slope <- function(x) {
  y <- y0 + b*(x - x0)
  return(y)
}

## Define a function Polynomial3: a cubic (degree 3) polynomial

Polynomial3 <- function(x) {
  y <- x^3 + 2*x^2 - 4*x - 1
  return(y)
}


x <- seq(0, 10, 0.1)
Plot_function(Line_point_slope, x)

Plot_function(Polynomial3, x)


Knowledge Point 2: Define and Plot Multivariable Functions in R

Example 2.2: The Tech Company’s Strategic Planning

InnovateTech Corp is optimizing its operations across multiple dimensions:

  1. Utility Function: \(U(x,y) = x^{0.6}y^{0.4}\) (consumer satisfaction from products x and y)
  2. Profit Function: \(\Pi(p_1,p_2) = (p_1-10)q_1 + (p_2-15)q_2\) where \(q_i = 100-2p_i\) (profit from two products)
  3. Production Function: \(Q(L,K) = 20L^{0.7}K^{0.3}\) (output from labor L and capital K)

The CEO asks: “How can we visualize these multivariable relationships in R to make better strategic decisions?”

Definition 2.1: Multivariable Function

A function \[f : \mathbb{R}^n \to \mathbb{R}\] is called a multivariable function if it maps multiple inputs \((x_1,x_2,...,x_n)\) to a real number \(f(x_1,x_2,...,x_n)\).

How to formulate and depict multivariable functional relationships

  • Consider the effect of one variable at a time while holding the others constant (ceteris paribus): is the relationship linear or nonlinear?
  • Are the combined effects of multiple variables additive (summing contributions) or multiplicative (interacting through products)?
  • Are there asymptotes or saturation effects (values the function approaches but does not exceed)?

Example of a linear function \[f(x,y) = x + 2y - 4\] This function takes two inputs \(x\) and \(y\) and returns a value that depends linearly on \(x\) and \(y\).

Example of a quadratic function \[f(x,y) =x^2+y^2\] \[f(x,y) = x^2+3xy+y^2\] These functions take two inputs \(x\) and \(y\) and return a quadratic expression (a sum of squares, possibly with a cross term).

How to plot multivariable functions in R

In plotting a univariable function \(y = f(x)\):

  1. Create a sequence of values for the input variable \(x\).
  2. Compute the corresponding values \(y=f(x)\).
  3. Plot the pairs \((x,y)\) and (optionally) connect them with a line.

For a multivariable function \(z=f(x,y)\) we use the same idea:

  1. Create sequences (a grid) of values for \(x\) and \(y\).
  2. Compute \(z=f(x,y)\) for each pair \((x,y)\).
  3. Plot the triples \((x,y,z)\) as a surface in 3D (or use a contour/heat map in 2D).

Visualise linear functions

\[f(x,y) = x+ 2y -4\] A function of two variables can be visualised as a surface over in a 3D coordinate system, where \((x,y)\) are the input variables and \(z\) is the value of the output variable. For the linear function the surface will be a plane.

As in the case of univariate functions, we create sequences for the input variables \(x\) and \(y\). For each pair \((x,y)\) we calculate the function value \(z=f(x,y)\). The resulting \(z\)-values are then connected to form the surface.

Surface plot

x <- seq(-10, 10, length.out = 30)
y <- seq(-10, 10, length.out = 30)

f <- function(x, y) { x + 2*y - 4 }
z <- outer(x, y, f)

#op <- par(bg = "white")
persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")

Contour Plot

A commonly used visualisation for a multivariable function is the contour plot. The link below shows terrain contour lines for the Snowy Mountains in Google Maps:

https://www.google.com/maps/place/Snowy+Mountains/@-36.5044076,148.3164928,14.6z/data=!4m6!3m5!1s0x6b234b038cf2dd5d:0x5e23945ff8d761b8!8m2!3d-36.5!4d148.3333333!16zL20vMDc0d20!5m2!1e4!1e3?entry=ttu&g_ep=EgoyMDI1MDgyNS4wIKXMDSoASAFQAw%3D%3D

Rice fields on hills create contour lines (level lines) in the landscape. Since rice needs water, the ridges around the fields must be kept even.

Rice field and Isoquants
Rice field and Isoquants

The rows of seats in a stadium are similar to the contour lines of a surface.

Along a contour line the height is constant, so contour lines are also called level lines. In economics, contour lines of a production function are called isoquants. The direction perpendicular to a contour line is the direction of steepest ascent (or descent).

contour(x, y, z, nlevels = 20)

Station stairs and Isoquants
Station stairs and Isoquants

Heat map

Heat map
Heat map

Heat maps are often used in weather forecasts to visualise temperature over an area (a function of longitude and latitude). The colour represents the function value (temperature).

For the linear function above:

filled.contour(x, y, z)

Gradient field

A heat map with contour lines shows where the surface is steep or flat. At any point, the direction perpendicular to the contour line is the direction of steepest ascent (and the opposite direction is steepest descent). A gradient field visualises this by drawing an arrow at each point that points in the direction of steepest ascent; longer arrows indicate a steeper slope.

plot(1, type = "n", xlim = range(x), ylim = range(y),
     xlab = "Labour (L)", ylab = "Capital (K)",
     main = "Gradient Field of Q(L,K)")


# Define partial derivatives (gradient components)
dQ_dx <- function(x, y) {
  1  # ∂Q/∂L
}

dQ_dy <- function(x, y) {
  2  # ∂Q/∂K
}


for(l in x) {
  for(k in y) {
    arrows(l, k, 
           l + dQ_dx(l, k)/8, 
           k + dQ_dy(l, k)/8,
           length = 0.04, col = "blue")
  }
}

On a plane, the direction of maximum ascent is the same everywhere; therefore, the arrows are identical at each point.

Interactive surface

Interactive surface plots allow you to rotate and zoom the 3D surface so you can view it from different perspectives.

library(plotly)
plot_ly(x = x, y = y, z = z, type = "surface")

Slice

A slice (cross-section) is a 2D plot obtained by fixing one input variable at a constant value, so the function depends on only one variable. In economics, slices are often used to study how the output changes with one input while holding the other input constant (ceteris paribus).

# --- Slice Plot ---
plot(x, f(x, y = 0), type = "l", col = "blue", lwd = 2,
     xlab = "Labour (L)", ylab = "Function value", main = "Slice of Plane Function (y = 0, 2, 4)")
lines(x, f(x, y = 2), col = "red", lwd = 2)
lines(x, f(x, y = 4), col = "green", lwd = 2)
legend("bottomright", legend = c("y = 0", "y = 2", "y = 4"), col = c("blue", "red", "green"), lwd = 2)

Visualisation of Nonlinear Functions

\[Q(L,K)=20L^{0.7}K^{0.3}\]

Surface plot

L <- seq(0, 10, length.out = 30)
K <- seq(0, 10, length.out = 30)

f <- function(L, K) { 20*L^0.7*K^0.3 }
z <- outer(L, K, f)

#op <- par(bg = "white")
persp(L, K, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")

Contour Plot

contour(L, K, z, nlevels = 20)

Heat Map

filled.contour(L, K, z)

Gradient Field

# Define partial derivatives (gradient components)
dQ_dL <- function(L, K) {
  20 * 0.7 * L^(-0.3) * K^0.3  # ∂Q/∂L
}

dQ_dK <- function(L, K) {
  20 * 0.3 * L^0.7 * K^(-0.7)  # ∂Q/∂K
}


plot(1, type = "n", xlim = range(L), ylim = range(K),
     xlab = "Labour (L)", ylab = "Capital (K)",
     main = "Gradient Field of Q(L,K)")

# Skip L = 0 or K = 0 to avoid infinite derivatives in the gradient.
for (l in L[L > 0]) {
  for (k in K[K > 0]) {
    arrows(l, k, 
           l + dQ_dL(l, k)/100, 
           k + dQ_dK(l, k)/100,
           length = 0.04, col = "blue")
  }
}

In a gradient field plot, the direction of an arrow points to the direction of ascent, and the length of the arrow indicates the steepness of that ascent. The longer the arrow, the steeper the ascent.

Interactive surface

library(plotly)
plot_ly(x = L, y = K, z = z, type = "surface")

Slice

# --- Slice Plot ---
plot(L, f(L, K = 5), type = "l", col = "blue", lwd = 2,
     xlab = "Labour (L)", ylab = "Output", main = "Slice of Production Function (K = 5)")
lines(L, f(L, K = 2), col = "red", lwd = 2)
lines(L, f(L, K = 1), col = "green", lwd = 2)
legend("bottomright", legend = c("K = 5", "K = 2","K = 1"), col = c("blue", "red","green"), lwd = 2)

Exercise

Visualise the following functions using surface plot, contour plot, heat map, slice, and 3D interactive surface plot.

  • A plane that passes through \((x_0,y_0) = (2,3)\) with slopes \(a=1.5\) in the \(x\)-direction and \(b=3\) in the \(y\)-direction. (Hint: the plane function is \(f(x,y) = f(x_0,y_0) + a(x-x_0) + b(y-y_0)\).)

  • \(f(x,y) = x^2 + y^2\)

using the following R Environment or your own local RStudio.

Visualisation Techniques

    1. Surface plots: Show the 3D shape of the function
    1. Contour plots: Show level curves (isoquants, indifference curves)
    1. Heat maps: Color-coded representation of function values
    1. Gradient field: Show the direction and steepness of ascent at each point
    1. Cross-sections: 2D slices of the 3D function

Economic Interpretations

  • Isoquants: Curves of constant output in production functions
  • Indifference curves: Curves of constant utility
  • Iso-profit curves: Curves of constant profit
  • Gradients: Direction of steepest increase

Interactive Quiz 2.1

Knowledge Point 3: Linear Equation Systems and Matrix Notation

Example 1.4: The Market Equilibrium (Revisited)

Finding where supply equals demand:

  • Supply Function: Q = -100 + 2P (suppliers willing to sell)
  • Demand Function: Q = 300 - P (consumers willing to buy)

This requires us to solve this system of two linear equations at the same time.

Example 2.3: The Manufacturing Company

TechProd Manufacturing produces three products: smartphones (S), tablets (T), and laptops (L). The company needs to determine optimal production levels based on resource constraints.

Resource Requirements per Unit:

  • Labour hours: 2S + 3T + 4L = 1000 hours available

  • Materials (kg): 1S + 2T + 3L = 600 kg available

  • Machine time: 3S + 1T + 2L = 800 hours available

This real-world problem requires us to solve a system of three linear equations at the same time.

How do we proceed? First recall how we solve a single linear equation:

\[ax=b \quad (a\neq 0)\]

We can divide both sides by \(a\) (equivalently, multiply both sides by \(a^{-1}\)):

\[a^{-1}ax = a^{-1}b\]

This leads to the solution:

\[x = a^{-1}b\]

For \(a=3\) and \(b=2\), the R solution is as follows:

a <- 3
b <- 2

solve(a) * b
##           [,1]
## [1,] 0.6666667

Can we follow the same procedure to solve a system of linear equations? The answer is yes. Here is how.

Example 2.4: The Manufacturing Company

Step 1: Move all unknowns to the left-hand side and the known constants to the right-hand side. Arrange the unknown variables in a consistent order (here: \(S\) first, \(T\) second, and \(L\) third).

\[2S + 3T + 4L = 1000\] \[1S + 2T + 3L = 600\] \[3S + 1T + 2L = 800\] Step 1.1: Write the unknowns as a vector:

Put all unknowns into one sequence as a vector \(X=\left( \begin{array}{c} S\\ T\\ L\\ \end{array} \right)\)

and the known constants on the right-hand side as a vector \(b=\left( \begin{array}{c} 1000\\ 600\\ 800\\ \end{array} \right)\)

Step 1.2: Put the coefficients of the system into a matrix:

\(A=\left( \begin{array}{ccc} 2&3&4\\ 1&2&3\\ 3&1&2\\ \end{array} \right)\)

Note that for a system of 3 equations we have 3 unknowns, so the vector \(X\) has 3 elements: \[\begin{array}{c} S\\ T\\ L\\ \end{array}\]

)$; and the right-hand-side constants form the vector \(b=\left( \begin{array}{c} 1000\\ 600\\ 800\\ \end{array} \right)\);

The coefficient matrix is a \(3\times 3\) square matrix. Its entries must match the chosen order of variables (this is why the order of variables matters).

\[\underbrace{\left( \begin{array}{rrr} 2 & 3 & 4\\ 1 & 2 & 3\\ 3 & 1 & 2\\ \end{array} \right)}_{A} \underbrace{\left( \begin{array}{c} S\\ T\\ L\\ \end{array} \right)}_{X} = \underbrace{\left( \begin{array}{r} 1000\\ 600\\ 800\\ \end{array} \right)}_{b} \]

Step 2: Multiply both sides by the inverse of the coefficient matrix \(A\) (assuming \(A\) is invertible).

\[A^{-1}AX = A^{-1}b\] Since \(A^{-1}A=I\), this leads to:

\[X = A^{-1}b\]

R solution is as follows:

A <- matrix(c(2,3,4,1,2,3,3,1,2),3,3,byrow = TRUE)
b <- c(1000, 600, 800)

solve(A)%*%b
##              [,1]
## [1,] 2.000000e+02
## [2,] 2.000000e+02
## [3,] 5.684342e-14
# %*% is the symbol of matrix multiplication in R.
# *   is the symbol of real number multiplication. 

The solution is \(S=200\), \(T=200\), and \(L=0\).

Exercise 2.3: The Market Equilibrium (Revisited)

  • Supply Function: Q = -100 + 2P
  • Demand Function: Q = 300 - P

After moving the unknowns to the left-hand side, we can write the system in matrix form:

\[\underbrace{\left( \begin{array}{rr} 1 & -2 \\ 1 & 1 \\ \end{array} \right)}_{A} \underbrace{\left( \begin{array}{c} Q\\ P\\ \end{array} \right)}_{X} = \underbrace{\left( \begin{array}{r} -100\\ 300\\ \end{array} \right)}_{b} \]

R solution

A <- matrix(c(1, -2, 1, 1), 2, 2, byrow = TRUE)
b <- c(-100, 300)
solve(A) %*% b
##          [,1]
## [1,] 166.6667
## [2,] 133.3333
# %*% is the symbol of matrix multiplication in R.
# *   is the symbol of real number multiplication. 

This is exactly the same solution we have obtained by substitution method in the previous section.

Existence and number of solutions (2 equations in 2 unknowns)

  • One solution: The two lines intersect at a single point. This happens when the slopes are different (equivalently, \(A\) is invertible and \(\det(A)\neq 0\)).
  • Infinitely many solutions: The two equations describe the same line (one equation is a multiple of the other). Here \(A\) is not invertible and \(\det(A)=0\).
  • No solution: The lines are parallel but distinct (same slope, different intercept). Here \(A\) is not invertible and \(\det(A)=0\).
Existence of solutions
Existence of solutions

Exercise

Solve the market equilibrium problem

  • Supply Function: Q = -80 + 2P
  • Demand Function: Q = 200 - 2P

Solve the problem above using the R environment below (or your own local RStudio).

Formal Presentation of Linear Equation Systems

Definition 2.2: Linear Equation System

A system of \(m\) linear equations in \(n\) unknowns has the form: \[\begin{cases} a_{11}x_1 + a_{12}x_2 + \cdots + a_{1n}x_n = b_1 \\ a_{21}x_1 + a_{22}x_2 + \cdots + a_{2n}x_n = b_2 \\ \vdots \\ a_{m1}x_1 + a_{m2}x_2 + \cdots + a_{mn}x_n = b_m \end{cases}\]

Matrix Representation

This system can be written compactly as: \(\mathbf{Ax} = \mathbf{b}\)

Where: - \(\mathbf{A} = \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{bmatrix}\) ,\(\hspace{0.5cm}\) \(\mathbf{x} = \begin{bmatrix} x_1 \\ x_2 \\ \vdots \\ x_n \end{bmatrix}\) ,\(\hspace{0.5cm}\) \(\mathbf{b} = \begin{bmatrix} b_1 \\ b_2 \\ \vdots \\ b_m \end{bmatrix}\)

Solution

\[\mathbf{x} = \mathbf{A}^{-1}\mathbf{b}\]

Matrix and Vector Operations and Geometric Interpretation

We have been using vector operations in R since the previous section. The example above—solving systems of linear equations using matrix notation—demonstrates the usefulness of vector and matrix notation. Now we formalise these operations and highlight some of the additional benefits of working with vectors and matrices.

    1. Vector Addition:\((\mathbf{x}+\mathbf{b})_i = x_i+b_i\)
    1. Vector Multiplication with a Number:\((a\mathbf{x})_i = ax_i\)
    • Scaling and potential direction reversal. \(\mathbf{x}\) and \(\mathbf{y}\) are parallel if \(\mathbf{x}=a\mathbf{y}\).
    1. Inner Product of Two Vectors:\(\mathbf{x}'\mathbf{y} = \sum_{k=1}^nx_ky_k\)
    • Measuring alignment/projection between vectors. \(\mathbf{x}'\mathbf{y}=0\), or \(\begin{bmatrix} x_1 & x_2 \end{bmatrix}\begin{bmatrix} y_1 \\ y_2 \end{bmatrix}=0\) implies the two vectors are perpendicular to each other.
    1. Outer Product of Two Vectors: \((\mathbf{x}\mathbf{y}^\top)_{i,j} = x_i y_j\)
    1. Matrix Addition: \((\mathbf{A} + \mathbf{B})_{ij} = a_{ij} + b_{ij}\)
    1. Matrix Multiplication: \((\mathbf{AB})_{ij} = \sum_{k=1}^{p} a_{ik}b_{kj}\)
    1. Matrix Multiplication With a Vector: \((\mathbf{Ax})_{i} = \sum_{k=1}^{n} a_{ik}x_{k}\)
    • From the rule of matrix–vector multiplication, a system of linear equations \[\begin{cases} a_{11}x_1 + a_{12}x_2 + \cdots + a_{1n}x_n = b_1 \\ a_{21}x_1 + a_{22}x_2 + \cdots + a_{2n}x_n = b_2 \\ \vdots \\ a_{m1}x_1 + a_{m2}x_2 + \cdots + a_{mn}x_n = b_m \end{cases}\] can be written as: \[\mathbf{Ax} = \mathbf{b} \]

Properties: - Associative: \((\mathbf{AB})\mathbf{C} = \mathbf{A}(\mathbf{BC})\) - Distributive: \(\mathbf{A}(\mathbf{B} + \mathbf{C}) = \mathbf{AB} + \mathbf{AC}\) - Not commutative: \(\mathbf{AB} \neq \mathbf{BA}\) (in general)

Two important relations between two vectors

  • Two vectors are parallel to each other: \(\mathbf{x} = \lambda \mathbf{y}\) or \[\begin{bmatrix} x_1 \\ x_2 \end{bmatrix}=\lambda \begin{bmatrix} y_1 \\ y_2 \end{bmatrix}\].

  • Two vectors are perpendicular to each other: \(\mathbf{x}'\mathbf{y} = 0\) or \[\begin{bmatrix} x_1 & x_2 \end{bmatrix} \begin{bmatrix} y_1 \\ y_2 \end{bmatrix}=0\].

Exercise

A vector with \(n\) elements is called an \(n\)-vector. A matrix with \(m\) rows and \(n\) columns is called an \(m\times n\) matrix. Verify the following statements using the matrix operation rules above.

  • Sum of two \(n\)-vectors is an \(n\)-vector.
  • An \(n\)-vector is an \(n\times 1\) matrix.
  • Inner product of two vectors is a number.
  • Outer product of an \(m\)-vector with an \(n\)-vector is an \(m\times n\) matrix.
  • Sum of two \(m\times n\) matrices is a \(m\times n\) matrix.
  • Product of an \(m\times n\) matrix and an \(n\times l\) matrix is an \(m\times l\) matrix.
  • Product of an \(m\times n\) matrix and an \(n\)-vector is an \(m\)-vector.

Interactive Quiz 2.2


Inverse Matrix and Solving Linear Equation Systems

Formal Presentation

Definition 2.3: Matrix Inverse

For a square matrix \(\mathbf{A}\), the inverse matrix \(\mathbf{A}^{-1}\) satisfies: \[\mathbf{A}\mathbf{A}^{-1} = \mathbf{A}^{-1}\mathbf{A} = \mathbf{I}\]

where \(\mathbf{I}\) is the identity matrix.

Existence Conditions

\(\mathbf{A}^{-1}\) exists if and only if: 1. \(\mathbf{A}\) is square (\(n \times n\)) 2. \(\det(\mathbf{A}) \neq 0\) (A is non-singular)

Interpretation

  • Determinant \(\neq 0\): The system has a unique solution (a unique intersection point).
  • Determinant \(= 0\): The system has either no solution or infinitely many solutions.

Geometric explanation (in 2D): \(\det(A)=0\) means the two equations are linearly dependent, so the lines are either parallel or identical.

  • If the lines are parallel with different intercepts, there is no intersection \(\to\) no solution.
  • If the lines are identical (same slope and intercept), there are infinitely many solutions.

Computing the Inverse

Using pencil and paper for \(2\times 2\) matrices: \[\mathbf{A}^{-1} = \frac{1}{\det(\mathbf{A})} \begin{bmatrix} d & -b \\ -c & a \end{bmatrix}\] where \(\mathbf{A} = \begin{bmatrix} a & b \\ c & d \end{bmatrix}\) and \(\det(\mathbf{A}) = ad - bc\)

For \(\mathbf{A} = \begin{bmatrix} 2 & 3 \\ 3 & 5 \end{bmatrix}\) we have \(\det(\mathbf{A})=1\), so the formula gives \(\mathbf{A}^{-1} = \begin{bmatrix} 5 & -3 \\ -3 & 2 \end{bmatrix}\).

Using R

### assign the matrix
A <- matrix(c(2, 3, 3, 5), 2, 2, byrow = TRUE)
### calculate the determinant
det(A)
## [1] 1
solve(A)
##      [,1] [,2]
## [1,]    5   -3
## [2,]   -3    2

Computing an inverse matrix by hand is practical only for very small matrices such as \(2\times 2\) matrices. For larger matrices, it is almost always done using a computer.

Exercise

Calculate the determinant and the inverse of \(\mathbf{A} = \begin{bmatrix} 2 & 3 \\ 3 & 5 \end{bmatrix}\) using the following R Environment or your local RStudio.

Geometric illustration of existence of solutions in a system of linear equations

  • One solution: Three planes intersect at a single point. This happens when \(A\) is invertible (equivalently, \(\det(A)\neq 0\)).
  • Infinitely many solutions: At least one plane is redundant (one equation is a combination of the others). Here \(A\) is not invertible and \(\det(A)=0\).
  • No solution: The planes do not share a common intersection point (e.g., at least two planes are parallel but distinct). Here \(A\) is not invertible and \(\det(A)=0\).

Interactive Quiz 2.3


Matrix Eigenvectors and Eigenvalues (Mini-module)

Estimated time: ~10–15 minutes
(If time is tight in a 90-minute class, you can treat the next subsection “Solving a nonlinear equation system” as optional self-study.)

Example 2.4B: The Language School vs. The Engineering School

Hook: Imagine two schools. Same number of study hours. Same three subjects. Yet students come out with very different strengths depending on which school they attend.
This story helps us understand one of the most useful ideas in matrix algebra: eigenvectors and eigenvalues.

A student’s abilities = a vector

We describe a student’s current strengths as a vector:

\[ \mathbf{s}= \begin{bmatrix} \text{Math}\\ \text{English}\\ \text{History} \end{bmatrix} \]

  • Student A (Math kid): \((8,3,2)\)
  • Student B (Humanities kid): \((2,8,7)\)

A school’s curriculum = a matrix

A curriculum can be modeled as a matrix transformation: it takes current abilities and produces new abilities.

  • Lumina (Language School, integrated humanities): \[ A_L= \begin{bmatrix} 4 & 1 & 1\\ 1 & 10 & 3\\ 1 & 3 & 6 \end{bmatrix} \] What the numbers mean: In Math class (6 hours), you spend 4 hours on pure math, 1 hour on word problems (English), and 1 hour on math history. You have 6 hours math, 14 hours English and 9 hours History

  • Structura (Engineering School, siloed specialization): \[ A_S= \begin{bmatrix} 14 & 0.5 & 0.5\\ 0 & 8 & 0\\ 0.5 & 0 & 6.5 \end{bmatrix} \] What the numbers mean: Math class is almost all math. English is pure English. History has a tiny math component. Subjects stay separate.

Interpretation (intuitive): the off-diagonal entries describe cross-training (e.g., doing half hour word problems, and half hour math history both of which contribute to your math ability.).

See it in action (matrix–vector multiplication)

Student A: The Math Kid \(M_k = (8, 3, 2)'\) at Lumina Language School.

\[A_L M_k = \begin{bmatrix} 4 & 1 & 1\\ 1 & 10 & 3\\ 1 & 3 & 6 \end{bmatrix} \begin{bmatrix} 8\\ 3 \\ 2 \end{bmatrix} =\begin{bmatrix} 37\\ 44 \\ 29 \end{bmatrix} \]

They became a humanities student! Their math dominance disappeared.

The Math Kid \(M_k = (8, 3, 2)'\) at Structura Engineering School.

\[A_S M_k = \begin{bmatrix} 14 & 0.5 & 0.5\\ 0 & 8 & 0\\ 0.5 & 0 & 6.5 \end{bmatrix} \begin{bmatrix} 8\\ 3 \\ 2 \end{bmatrix} =\begin{bmatrix} 114.5\\ 24 \\ 17 \end{bmatrix} \]

Their math strength exploded! They’re still a math kid, just stronger.

# Ability vectors (Math, English, History)
StudentA <- c(Math = 8, English = 3, History = 2)

# Curriculum matrices
Lumina <- matrix(c(4,1,1,
                   1,10,3,
                   1,3,6),
                 nrow = 3, byrow = TRUE,
                 dimnames = list(c("Math","English","History"),
                                 c("Math","English","History")))

Structura <- matrix(c(14,0.5,0.5,
                      0,8,0,
                      0.5,0,6.5),
                    nrow = 3, byrow = TRUE,
                    dimnames = list(c("Math","English","History"),
                                    c("Math","English","History")))

# Apply each school to each student
# Lumina %*% StudentA
# Structura %*% StudentA

A quick visual comparison for Student A (before vs after each school):

scores_A <- rbind(
  Original  = StudentA,
  Lumina    = as.vector(Lumina %*% StudentA),
  Structura = as.vector(Structura %*% StudentA)
)

colnames(scores_A) <- c("Math","English","History")

barplot(t(scores_A), beside = TRUE, legend.text = TRUE,
        main = "Student A: Abilities before and after each school",
        ylab = "Ability score", las = 1)

Key insight: the same student can end up with very different ability profiles depending on the matrix (the “system”) acting on them.

Are there students whose profile of abilities doesn’t change—it just gets amplified? This question leads to the concept of eigenvalue and eigenvector.

At Lumina meets Taylor whose ability profile is \(T = (1, 5.1, 2.8)'\).

\[A_L T = \begin{bmatrix} 4 & 1 & 1\\ 1 & 10 & 3\\ 1 & 3 & 6 \end{bmatrix} \begin{bmatrix} 1\\ 5.1 \\ 2.8 \end{bmatrix} =\begin{bmatrix} 11.9\\ 60.4 \\ 33.1 \end{bmatrix} \]

Lumina doesn’t change what kind of student Taylor is—it just makes Taylor 11.9 times stronger in every subject. The school is his “own” school. (Eigen means literally its own.)


Formal Definition: Eigenvectors and Eigenvalues

Most vectors change direction after a matrix transformation. But some “special” vectors keep the same direction and only get stretched or shrunk.

A nonzero vector \(\mathbf{v}\) is an eigenvector of a matrix \(A\) if:

\[A\mathbf{v} = \lambda \mathbf{v}\]

  • \(\mathbf{v}\) is the eigenvector (“special direction / stable pattern”)
  • \(\lambda\) is the eigenvalue (the “amplification factor”)

Important: if \(\mathbf{v}\) is an eigenvector, then any scaled version \(c\mathbf{v}\) (for \(c\neq 0\)) is also an eigenvector.


Finding the dominant eigenvector in R

Many matrices have multiple eigenvectors. In applications we often care about the dominant eigenvector: the one associated with the largest eigenvalue (largest growth/stretch).

#eigen(Lumina)
#eigen(Structura)

Interpretation (informal): - For Lumina, the dominant eigenvector has a relatively large English/History component → an “integrated humanities” pattern. - For Structura, the dominant eigenvector is heavily Math-dominated → a “specialist engineering” pattern.



Quick checks and takeaways

  • Can a matrix have more than one eigenvector? Yes. Most matrices have multiple eigenvectors (often one per dimension).
  • What if an eigenvalue is less than 1? Then the eigenvector direction is shrunk (the “pattern weakens”).
  • Is an eigenvector the “best” direction? Not necessarily. It is simply a direction that the system does not rotate.

Big picture for business: eigenvectors/eigenvalues describe “natural modes” of systems—useful in portfolio risk models, Markov chains, principal components, and many optimisation methods.

Exercise (short)

  1. Use eigen() to compute all eigenvalues of Lumina and Structura.
  2. Which school has the larger dominant eigenvalue? What does that suggest about “amplification” in this toy model?
  3. Pick the dominant eigenvector \(\mathbf{v}\) and numerically verify \(A\mathbf{v} \approx \lambda\mathbf{v}\) by checking the ratios of corresponding components.

Solving a nonlinear equation system (optional)

Solving a nonlinear system of equations is generally more complicated than solving a linear system. Geometrically, a nonlinear equation in two variables defines a curve in the \((x,y)\)-plane. A system of two nonlinear equations corresponds to two curves; their intersection points (if any) are the solutions. The interactive graph below lets you explore the solutions of a system of two nonlinear equations in two variables.


Knowledge Point 4: Quadratic Functions and Positive Definite Matrices

In business and economics, quadratic functions are of particular interest. Consider the one-variable quadratic: \[f(x) = ax^2\]

Question: When is this function always positive (except at \(x=0\))? (For example, when does the business activity guarantee a positive result?)

Answer: When \(a > 0\)

This is the one-variable case of positive definiteness: the parabola opens upward and has a minimum at \(x=0\).

Extending this to the multivariate case, consider \[f(\mathbf{x}) = \mathbf{x}^\top A \, \mathbf{x}= \sum_{i=1}^n\sum_{j=1}^n a_{ij}x_i x_j\] where \(\mathbf{x}\) is a vector and \(A\) is a square \(n\times n\) matrix. If this function is always positive for all \(\mathbf{x}\neq \mathbf{0}\), then \(A\) is called positive definite.

Example 2.5: Hooke’s law and spring energy

In physics, the potential energy stored in a stretched spring is always positive (unless there is no displacement):

\[ E = \frac{1}{2}\mathbf{x}^\top K\mathbf{x}\] where \(K\) is a stiffness matrix. For the system to make physical sense, energy must be positive \(\to\) \(K\) must be positive definite.

Example 2.6: The Investment Risk Analysis

Global Portfolio Management is analyzing investment risk using quadratic forms. The firm’s risk model for a two-asset portfolio is:

\[\text{Risk} = w_1^2\sigma_1^2 + w_2^2\sigma_2^2 + 2w_1w_2\sigma_{12}\]

Where: + \(w_1, w_2\) are portfolio weights

  • \(\sigma_1^2, \sigma_2^2\) are asset variances

  • \(\sigma_{12}\) is the covariance between assets

This can be written as a quadratic form: \[\text{Risk} = \mathbf{w}^T\mathbf{\Sigma}\mathbf{w}\] with \(\mathbf{w}=\left( \begin{array}{c} w_1\\ w_2\\ \end{array} \right)\) and \(\mathbf{\Sigma} =\left(\begin{array}{cc} \sigma_1^2& \sigma_{12}\\ \sigma_{12}& \sigma_2^2\\ \end{array} \right)\)

The risk manager asks: “How can we determine if our covariance matrix ensures the risk is always positive (positive definite), and how do we visualize this?”

Definition 2.4: Positive Definite Matrices

A symmetric matrix \(\mathbf{A}\) is positive definite if: \[\mathbf{x}^T\mathbf{A}\mathbf{x} > 0 \text{ for all } \mathbf{x} \neq \mathbf{0}\]

Equivalent conditions:

  1. All eigenvalues of \(\mathbf{A}\) are positive

  2. All leading principal minors are positive

  3. \(\mathbf{A} = \mathbf{L}\mathbf{L}^T\) for some invertible matrix \(\mathbf{L}\) (Cholesky decomposition)

Calculation of eigenvalues in R

A<- matrix(c(2,1,1,3),2,2,byrow=TRUE)
A
##      [,1] [,2]
## [1,]    2    1
## [2,]    1    3
eigen(A)
## eigen() decomposition
## $values
## [1] 3.618034 1.381966
## 
## $vectors
##           [,1]       [,2]
## [1,] 0.5257311 -0.8506508
## [2,] 0.8506508  0.5257311

Graphical Properties

  • Positive definite:
    • Elliptical contours, unique minimum
    • The surface bends upward
# Create a grid over [-10, 10]
x <- seq(-10, 10, length.out = 100)
y <- seq(-10, 10, length.out = 100)

z <- outer(x, y, function(x, y) {
  mapply(function(x, y) {
    X <- c(x, y)
    t(X) %*% A %*% X
  }, x, y)
})

filled.contour(x, y, z)

Interactive surface

# --- Interactive 3D Surface ---
library(plotly)
plot_ly(x = x, y = y, z = z, type = "surface")

Negative Definite Matrices

A symmetric matrix \(\mathbf{A}\) is negative definite if: \[\mathbf{x}^T\mathbf{A}\mathbf{x} < 0 \text{ for all } \mathbf{x} \neq \mathbf{0}\]

All eigenvalues of \(\mathbf{A}\) are negative

Calculation of eigenvalues in R

A<- matrix(c(-2,1,1,-3),2,2,byrow=TRUE)
A
##      [,1] [,2]
## [1,]   -2    1
## [2,]    1   -3
eigen(A)
## eigen() decomposition
## $values
## [1] -1.381966 -3.618034
## 
## $vectors
##            [,1]       [,2]
## [1,] -0.8506508 -0.5257311
## [2,] -0.5257311  0.8506508

Graphical Properties

  • Negative definite:
    • Elliptical contours, unique maximum
    • The surface bends downward
# Create a grid over [-10, 10]
x <- seq(-10, 10, length.out = 100)
y <- seq(-10, 10, length.out = 100)

z <- outer(x, y, function(x, y) {
  mapply(function(x, y) {
    X <- c(x, y)
    t(X) %*% A %*% X
  }, x, y)
})


filled.contour(x, y, z)

Interactive surface

# --- Interactive 3D Surface ---
library(plotly)
plot_ly(x = x, y = y, z = z, type = "surface")

Exercise

For \(A =\begin{bmatrix} -4 & -1 \\ -1 & -6 \end{bmatrix}\), create 3D plots of the quadratic function \(f(\mathbf{x}) = \mathbf{x}^\top A\mathbf{x}\) over the domain \(x_1\in[-1,1]\), \(x_2\in[-1,1]\) using the R environment below (or your own local RStudio).

Interactive Quiz 2.4


Comprehensive Review Questions

This section provides review and application questions designed to assess understanding at five different levels. Each level builds upon the previous one, fostering deeper comprehension and the ability to apply concepts in increasingly complex situations.

https://pchen-exercisesfeedbackselect.hf.space


Summary and Next Steps

This comprehensive Section 2 covers the essential mathematical foundations for business analysis:

  1. Linear systems and matrices - The backbone of resource allocation and constraint optimization
  2. Matrix inversion - Solving complex business systems efficiently
  3. Eigenvalues and eigenvectors - Identifying stable patterns (“natural modes”) in matrix models
  4. Composite and inverse functions - Understanding indirect relationships and growth models
  5. R programming for univariable functions - Practical tools for economic analysis
  6. Multivariable function visualisation - Advanced decision-making with multiple factors
  7. Quadratic forms and positive definite matrices - Risk analysis and optimization theory

Preparation for Section 3: These tools provide the foundation for understanding derivatives, which measure rates of change and enable optimization in business contexts.

Key Takeaways: - Mathematics provides powerful tools for business decision-making - Multiple representations (algebraic, graphical, computational) enhance understanding - R programming enables sophisticated analysis of real business problems - Interactive learning reinforces theoretical concepts with practical applications


Total estimated lecture time: 90 minutes
(Eigenvalues mini-module ~10–15 minutes; nonlinear-system visualization can be self-study if needed.) Interactive elements: 6 quizzes + 6 R environments Assessment: 30 comprehensive questions across 5 levels of understanding